#pragma once
#include "UITemplate.h"
#include <QtXml/QDomDocument>  
class CLoadUICfg
{
public:
	CLoadUICfg(QString strFileName);
	~CLoadUICfg();
	static   SharedPointer<CLoadUICfg>  GetInstance()
	{
		if (!m_pCfg.get())
		{
			m_pCfg = SharedPointer<CLoadUICfg>(new CLoadUICfg("..\\Data\\configureObj.xml"));
		}
		return m_pCfg;
	}

	bool GetObjRelationUI(QString objName, ObjRelationUI& relationUI) const;

	ZMVector<UITemplate> GetObjUITemplate(QString objName) const;

	ZMVector<QString> GetAllObject() const;

	ZMVector<QString> GetObjectNeedUpdate() const;

	void AddNewRelation(QString strObjName, UITemplate uiTemplate);

	void ResetUpdate();

	void UpdateData();

	void UpdateShowMesh(OgreWidget * pWidget);

	int FindTemplateUIFromObj(const QString& objName, const QString& uiRelationID, const QString& templateID, UITemplate&tempUI)
	{
		int iRet = 1;

		PUIRelationTemplate pRelation;
		if (uiRelationID == "" && m_objWithUI[objName].GetUIRelation().size() == 1)
		{
			pRelation = m_objWithUI[objName].GetUIRelation()[0];
		}
		else
		{
			for (PUIRelationTemplate pRelationTemp : m_objWithUI[objName].GetUIRelation())
			{
				if (pRelationTemp->GetId() == uiRelationID)
				{
					pRelation = pRelationTemp;
					break;
				}
			}
		}


		if (pRelation.get())
		{
			ZMVector<UITemplate> vTemplate = pRelation->GetTemplate();
			if (templateID.isEmpty() && vTemplate.size()==1)
			{
				iRet = 0;
				tempUI = vTemplate[0];

			}
			else
			{
				for (UITemplate& templateUI : vTemplate)
				{
					if (templateUI.GetTemplateID() == templateID && !templateID.isEmpty())
					{
						iRet = 0;
						tempUI = templateUI;
						break;
					}
				}
			}
		}

		ZMString idRelation;
		
		
		return iRet;
	}

	void InitObjInfoTree(QTreeView* pView);

	void GetBridgeProgressData(QTreeView* pView, map<QString , QDate>& vProgressData);

	void SaveProgressData(QTreeView* pView);
	
	void GetProgressDataScope(QDate&tFrom, QDate& tTo);

	void UpDateObjProgressWithTime(QDate t);
protected:
	void UpdateProgressData(const map<QString, QDate>& vProgressData);

	void LoadProgressData(map<QString, QDate>& vProgressData);
	
	QMap<QString, UITemplate> LoadUITemplate(const QDomDocument& doc);

	QMap<QString, SharedPointer<UIRelationTemplate>> LoadUIRelationTemplate(const QDomDocument& doc);

	QMap<QString, ObjRelationUI> LoadObjWithUI(const QDomDocument& doc);

	void SortData();
private:
	QMap<QString, QList<QString>> m_uiRelationTemplateWithObjName;
	QMap<QString, UITemplate> m_uiTemplate;
	QMap<QString, PUIRelationTemplate> m_uiRelationTemplate;
	QMap<QString, ObjRelationUI> m_objWithUI;

	QMap<QString, QModelIndex> m_objProgress;
	static SharedPointer<CLoadUICfg> m_pCfg;
};
typedef SharedPointer<CLoadUICfg> PLoadUICfg;
